package cn.lyjuan.first.netty.demo._3_netty5.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;

import java.net.SocketAddress;

public class ServerHandler extends SimpleChannelInboundHandler<String>
{
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception
    {
        System.out.println("server messageReceived: " + msg);

        // write
        ctx.channel().writeAndFlush("hi");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
    {
        System.out.println("server exceptionCaught");
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception
    {
        System.out.println("server connect");
        super.connect(ctx, remoteAddress, localAddress, promise);
    }

    @Override
    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
    {
        System.out.println("server disconnect");
        super.disconnect(ctx, promise);
    }
}
